package com.cloud.basic.reflection;


import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Random;

/**
 * @program: basic
 * @description: 自动控制温度
 * 反射是非常规开发手段，它会抛弃 Java 虚拟机的很多优化，所以同样功能的代码，
 * 反射要比正常方式要慢，所以考虑到采用反射时，要考虑它的时间成本。
 * 另外，就如无人驾驶之于汽车一样，用着很爽的同时，其实风险未知。
 * 所以说，反射固然好，切勿翻车！
 * @author: Cloud
 * @create: 2020/11/28 10:30:17
 */
public class AutoControlTemp {

    // 室内温度等级
    public enum Temperature{
        HighTemp(37),
        LowTemp(36);
        private Integer temp;

        Temperature(Integer temp) {this.temp = temp;}
    }

    // 谁开启了空调
    private String name;

    // 空调型号
    private String brand;

    public AutoControlTemp(String name,String brand) {
        super();
        this.name = name;
        this.brand = brand;
    }

    public void run(){
        System.out.println(this.name + "开启"+this.brand+"空调,自动检测开启！");
    }

    public void auto(int temp){
        if (Temperature.HighTemp.temp<temp) {
            decreaseTemp();
        } else if (Temperature.LowTemp.temp>temp) {
            increaseTemp();
        } else {
            keepTemp();
        }
    }

    public void decreaseTemp(){
        System.out.println("温度过高，打开自动降温~");
    }

    public void increaseTemp(){
        System.out.println("温度过低，打开自动升温~");
    }

    public void keepTemp(){
        System.out.println("温度适合，不用调节");
    }

    public static void main(String[] args) {
        // 定义当前温度
        // 第一种
        int temp = 0;
//        AutoControlTemp controlTemp = new AutoControlTemp("宁瑶","美的");
//        controlTemp.run();
        Random r = new Random(1);
//        for (int i = 0; i < 10; i++) {
//            try {
//                Thread.sleep(10000);
//            } catch (InterruptedException e) {
//                e.printStackTrace();
//            }
//            temp = r.nextInt(100);
//            System.out.println(temp);
//            controlTemp.auto(temp);
//        }

        // 反射获取Class
        Class clz = AutoControlTemp.class;
        try {
            // 获取构造方法
            Constructor constructor = clz.getConstructor(String.class, String.class);

            AutoControlTemp autoControlTemp = (AutoControlTemp)constructor.newInstance("宁瑶", "美的");

            Method method = clz.getMethod("run");

            method.invoke(autoControlTemp,null);

            Method method1 = clz.getMethod("auto", int.class);

            for (int i = 0; i < 10; i++) {
                try {
                    Thread.sleep(10000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                temp = r.nextInt(100);
                System.out.println(temp);
                method1.invoke(autoControlTemp,temp);
            }

        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
